﻿using ControlHelper.Attributes;
using Creator.Core.Base;
using Creator.Core.Log;
using System;
using System.Windows.Media;

namespace Creator.Core.Component
{
    [ComponentMenu("渲染/画布组件")]
    public class CanvasComponent : RenderComponent  
    {
        private Color backColor = Colors.Gray;
        private SolidColorBrush colorBrush;
        private double width = 1920;
        private double height = 1080;
        private float intervarl;
        private double currentTime = 0;
        public CanvasComponent(BaseObject baseObject) : base(baseObject)  {    }
        public override string Name => "画布组件";
        [NickName("背景颜色")] 
        public Color BackColor
        {
            get => backColor; set {  
                backColor = value; 
                BackBrush = new SolidColorBrush(backColor);
            } 
        } 
        [Hide]
        public SolidColorBrush BackBrush 
        {
            get
            {
                return colorBrush;
            }
            set
            {
                colorBrush = value;
                RaisePropertyChanged();
            }
        }
       
        [NickName("宽度")]
        public double Width
        {
            get => width; set
            {
                width = value;
                RaisePropertyChanged();
            }
        }
        [NickName("高度")]
        public double Height
        {
            get => height; set
            {
                height = value;
                RaisePropertyChanged();
            }
        }
        [NickName("刷新间隔时间(秒)")]
        public float Intervarl
        {
            get => intervarl; set
            {
                intervarl = value;
            }
        }


        [NickName("需要展示的数据")]
        public DataListComponent DataList { get; set; }

 

        public override event Action<object, object> OnInit;
        public override event Action<object, object, double> OnRun;
        public override event Action<object, object> OnEditorEvent;
        public override void Init()
        {
            base.Init();
            if (DataList == null)
            {
                ShowMsg("数据组件为空，画布组件在初始化不会获取显示数据",   LogLevel.Warning);
                return;
            }
            if (DataList.Data == null)
            {
                ShowMsg("数据组件数据为空，画布组件在初始化不会获取显示数据", LogLevel.Warning);
                return;
            }
            OnInit?.Invoke(this, DataList.Data);
        }

        public override void Run(double deltaTime)
        {
            if (DataList == null)
            {
                ShowMsg("数据组件为空，画布组件在运行过程中不会刷新显示数据", LogLevel.Warning);
                return;
            }

            if (Entrey.realTime > currentTime + Intervarl)
            {
                OnRun?.Invoke(this, DataList.Data, deltaTime);
                base.Run(deltaTime);
                currentTime = Entrey.realTime;
            }

        } 
    }
}
